//
//  NewAddressSexView.m
//  FD
//
//  Created by xiaofang wang on 2018/10/29.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "NewAddressSexView.h"
@interface NewAddressSexView()
@property (nonatomic, strong) UIButton *manBtn;
@property (nonatomic, strong) UIButton *womanBtn;

@end

@implementation NewAddressSexView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
        self.backgroundColor = [UIColor whiteColor];

        //一次只能保证一个被选中
//        self.manBtn.selected = YES; //默认选中 //1-19没有默认选中
    }
    return self;
}
- (void)setupView
{
    CGFloat sh = self.bounds.size.height;
    CGFloat h = 20;
    CGFloat y = (sh - h) * 0.5;
    
    //国际化
    NSString *man   = NSLocalizedString(Home_Man, @"先生");
    NSString *woamn = NSLocalizedString(Home_Woman, @"女士");
    
    self.manBtn = [self createBtnWithAction:@selector(clickManAction:) title:man];
    self.manBtn.frame  =CGRectMake(115, y, 60, h);
    [self.manBtn leftTextRightImageWithMargin:5];//间距5
    [self addSubview:self.manBtn];
    
    
    self.womanBtn = [self createBtnWithAction:@selector(clickWoamnAction:) title:woamn];
    self.womanBtn.frame = CGRectMake(self.manBtn.rb_right + 20, y, 60, 20);
    [self.womanBtn leftTextRightImageWithMargin:10];//间距5
    [self addSubview:self.womanBtn];
    
    
    //2019-1-19 设置字号
    [self.manBtn.titleLabel setFont:[UIFont systemFontOfSize:15.0]];
    [self.womanBtn.titleLabel setFont:[UIFont systemFontOfSize:15.0]];
}

- (UIButton *)createBtnWithAction:(SEL)action title:(NSString *)title {
    UIButton *btn  = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [btn setTitle:title forState:(UIControlStateNormal)];
    [btn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [btn setImage:[UIImage imageNamed:@"unselected"] forState:(UIControlStateNormal)];
    [btn setImage:[UIImage imageNamed:@"selected"] forState:(UIControlStateSelected)];
    
    [btn addTarget:self action:action forControlEvents:(UIControlEventTouchUpInside)];

    return btn;
}

- (void)clickManAction:(UIButton *)btn
{
    FDLog(@"%@",btn.titleLabel.text);
    btn.selected = YES;
    self.womanBtn.selected  = NO;
    if (self.clickSexViewBlock) { //选择的是男 0
        self.clickSexViewBlock(@(0));
    }
    
//    //单选：只有一个被选中
//    if (btn.selected) {
//        self.womanBtn.selected = NO;
//        NSLog(@"选中的是：%@",btn.titleLabel.text);
//
//        if (self.clickSexViewBlock) { //选择的是男 0
//            self.clickSexViewBlock(@(0));
//        }
//
//    }else {
//        self.womanBtn.selected = YES;
//
//        if (self.clickSexViewBlock) { //选择的是女 1
//            self.clickSexViewBlock(@(1));
//        }
//    }
}

- (void)clickWoamnAction:(UIButton *)btn {
    FDLog(@"%@",btn.titleLabel.text);
    
    btn.selected = YES;
    self.manBtn.selected = NO;
    if (self.clickSexViewBlock) {
        self.clickSexViewBlock(@(1));
    }
    
//    btn.selected = !self.manBtn.selected;
//    btn.selected = !btn.selected;
//
//    //单选：只有一个被选中
//    if (btn.selected) {
//        self.manBtn.selected = NO;
//        NSLog(@"选中的是：%@",btn.titleLabel.text);
//
//        if (self.clickSexViewBlock) { //选择的是 女 1
//            self.clickSexViewBlock(@(1));
//        }
//    }else {
//        self.manBtn.selected = YES;
//        if (self.clickSexViewBlock) { //选择的是 男 0
//            self.clickSexViewBlock(@(0));
//        }
//    }
}


- (void)updateSexBtnWithSexNumber:(NSNumber *)sex {
    if ([sex isEqualToNumber:@0]) { //先生
        self.manBtn.selected = YES;
        self.womanBtn.selected = NO;
    }else if([sex isEqualToNumber:@1])  { //女士
        self.womanBtn.selected = YES;
        self.manBtn.selected = NO;
    }
}

@end
